import {
  BookingDataRes,
  IdentityDataRes,
  LpnDataRes,
} from "@/components/booking-record/types";
import { create } from "zustand";
import { persist } from "zustand/middleware";

interface UserStoreState {
  userId: string;
  userStatus: string;
  identityData: IdentityDataRes;
  lpnData: LpnDataRes[];
  bookingData: BookingDataRes[];
  redDotVisible: boolean;
  checkPendingStatus: () => void;
  updateUserId: (newId: string) => void;
  updateIdentityData: (newData: IdentityDataRes) => void;
  updateLpnData: (newLpnData: LpnDataRes[]) => void;
  updateBookingData: (newBookingData: BookingDataRes[]) => void;
  updateUserStatus: (userStatus: string) => void;
}

const useUserStore = create<UserStoreState>()(
  persist(
    (set) => ({
      userId: "",
      userStatus: "",
      identityData: {
        hkiaId: "",
        approvalStatus: "",
        reason: "",
        expireDate: "",
        englishName: "",
        chineseName: "",
        email: "",
        mobile: NaN,
        companyId: "",
      },
      lpnData: [],
      bookingData: [],
      redDotVisible: false,
      checkPendingStatus: () => {
        set((state) => {
          const hasPending =
            state.identityData.approvalStatus === "PENDING" ||
            state.lpnData.some((item) => item.approvalStatus === "PENDING") ||
            state.bookingData.some((item) => item.bookingStatus === "PENDING");
          return { redDotVisible: hasPending };
        });
      },
      updateUserId: (newId) => set({ userId: newId }),
      updateIdentityData: (newData) => set({ identityData: newData }),
      updateLpnData: (newLpnData) => set({ lpnData: newLpnData }),
      updateBookingData: (newBookingData) =>
        set({ bookingData: newBookingData }),
      updateUserStatus: (userStatus) => set({ userStatus }),
    }),
    {
      name: "user-storage", // 持久化存储的名称
    }
  )
);

export default useUserStore;
